package com.example.ujsfish.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.ujsfish.R;
import com.example.ujsfish.adapter.PoolPointAdapter;
import com.example.ujsfish.model.monitor.Data;
import com.example.ujsfish.model.monitor.JsonRootBean;
import com.example.ujsfish.model.poolMap.PoolPoint;
import com.example.ujsfish.utils.AppNetService;
import com.example.ujsfish.utils.DeviceName;
import com.example.ujsfish.utils.RetrofitManager;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class PaintMapByMan extends AppCompatActivity {

    RetrofitManager myRetrofitManager = RetrofitManager.getInstance();
    Retrofit myRetrofit = RetrofitManager.getRetrofit();
    AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);

    private List<PoolPoint> poolList = new ArrayList<>(); //池塘顶点

    private Button btn_insertPoint;
    private Button btn_uploadBoundary;
    private Button btn_deletePre;
    private EditText X_editText;
    private EditText Y_editText;
    private EditText poolName_editText;
    private ListView pointName_list;
    private String id;
    private int count1;
    private String TAG = "PaintMapByMan";
    private String poolName;
    private String userName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paint_map_by_man);
        id = DeviceName.getDeviceInstance().getDeviceId().get(0);
        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
         userName = preferences.getString("userName", "");
        initView();
        initListener();

    }

    private void initListener() {
        btn_deletePre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count1--;
                poolList.remove(poolList.size()-1);
                initListView(pointName_list, poolList);
            }
        });

        //添加边界点
        btn_insertPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String checkString = "[ESWN]\\d\\d\\d.\\d";
                String X = X_editText.getText().toString().trim();
                String Y = Y_editText.getText().toString().trim();
                if (!(X.length() == 6 && Y.length() == 6)){
                    if(X.matches(checkString)){
                        Toast.makeText(getApplication(),"坐标点格式错误，请重新输入",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    return;
                }
                count1++;
                poolList.add(new PoolPoint(count1,true ,X ,Y));
                initListView(pointName_list, poolList);
                System.out.println(poolList.toString());
            }
        });


        //提交边界点
        btn_uploadBoundary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(poolName_editText.getText().toString().isEmpty()){
                    Toast.makeText(getApplication(),"请输入池塘名",Toast.LENGTH_SHORT).show();
                    return;
                }
                poolName = poolName_editText.getText().toString();

                StringBuilder str = new StringBuilder();
                for (PoolPoint poolPoint :
                        poolList) {
                    str.append(poolPoint.getPoint_X());
                    str.append(",");
                    str.append(poolPoint.getPoint_Y());
                    str.append(";");
                }
                Call<ResponseBody> addBoundary = appNetService.postAddBoundary(poolName, "Boundary", str.toString(),userName);
                addBoundary.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        Toast.makeText(getApplication(),"添加池塘成功",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Toast.makeText(getApplication(),"添加池塘失败",Toast.LENGTH_SHORT).show();
                        t.printStackTrace();
                    }
                });

                poolList.clear();
                initListView(pointName_list, poolList);

                count1 = 0;
                poolName_editText.setText("");

            }
        });

    }

    private void initView() {
         btn_insertPoint = findViewById(R.id.btn_insertPoint);
         btn_uploadBoundary = findViewById(R.id.btn_uploadBoundary);
        btn_deletePre = findViewById(R.id.btn_deletePre);
         X_editText = findViewById(R.id.X_editText);
         Y_editText = findViewById(R.id.Y_editText);
         poolName_editText = findViewById(R.id.poolName_editText);
         pointName_list = findViewById(R.id.pointName_list);
    }

    private void initListView( ListView listViewPool, List<PoolPoint> poolList) {
        PoolPointAdapter adapter1 = new PoolPointAdapter(PaintMapByMan.this, R.layout.pool_point_item, poolList);
        listViewPool.setAdapter(adapter1);
        adapter1.notifyDataSetChanged();
    }

}